#include <stdio.h>
#include <stdlib.h> /*needed to convert a string to an integer*/
#define MAXCHARS 40
#define TRUE 1
#define FALSE 0

int isvalidInt(char[]); /*function prototype*/

int main()
{

    char value[MAXCHARS];
    int number;

    printf("Enter an integer: ");
    gets(value);

    if (isvalidInt(value) == TRUE)
    {
        number = atoi(value);
        printf("The number you entered is %d\n", number);
    }
    else
        printf("The number you entered is not a valid integer.\n");

    return 0;
}

int isvalidInt(char val[])
{
    int start = 0;
    int i;
    int valid = TRUE;
    int sign = FALSE;

    /*check for an empty string*/
    if (val[0] == '\0')
        valid = FALSE;

    /*check for a leading sign*/
    if (val[0] == '-' || val[0] == '+')
    {
        sign = TRUE;
        start = 1; /*start checking for digits after the sign*/
    }

    /*check that there is at least one character after the sign*/
    if (sign == TRUE && val[1] == '\0')
        valid = FALSE;

    /*now check the string, which we know has at least one non-sign char*/
    i = start;
    while (valid == TRUE && val[i] != '\0')
    {
        if (val[i] < '0' || val[i] > '9') /*check for a non-digit*/
            valid = FALSE;
        i++;
    }

    return valid;
}